#include <stdio.h>
#include "example1.h"
#include "openFSM.h"

/*  begin   : @#!&
 *  end     : &!#@
 *  number  : sizeof(info) + sizeof(check)
 *  SUM:      'begin' 'number' 'info' 'check' 'end'
 *  example : @#!&000011ABCDEFGHIJ'0B'&!#@
*/
unsigned char NeedParseData[100] = {};



/* Types of events */
enum eventType {
   to_Error,
   to_Begin,
   to_Number,
   to_Info,
   to_Check,
   to_End
}tcpParse;

static int ALL_TO_ERROR(void)
{
    printf("Parse STATE begin\n");
    return 1;
}

static int ALL_TO_ALL(void)
{
    printf("everything to next state\n");
    return 1;
}

static int IDLE_TO_RUN(void)
{
    printf("IDLE TO RUN\n");
    return 1;
}

static void Parse_Begin_activeStatus(void * stateData)
{
    printf("Parse STATE begin\n");
}



struct FSM_state Parse_ERROR, Parse_BEGIN, Parse_NUMBER, Parse_INFO, Parse_CHECK, Parse_END;

struct FSM_state Parse_ERROR = {
    .name = "error",
    .entryAction = NULL,
    .continuousAction = NULL,
    .exitAction = NULL,
    .data = NULL,
    .transitions = (struct transition[]){
        {to_Begin, bool_True, ALL_TO_ALL, &Parse_BEGIN}
    },
    .numTransitions = 1
};

struct FSM_state Parse_BEGIN = {
    .name = "begin",
    .entryAction = NULL,
    .continuousAction = Parse_Begin_activeStatus,
    .exitAction = NULL,
    .data = NULL,
    .transitions = (struct transition[]){
        {to_Number, bool_True, ALL_TO_ALL, &Parse_NUMBER}
    },
    .numTransitions = 1
};

struct FSM_state Parse_NUMBER = {
    .name = "number",
    .entryAction = NULL,
    .continuousAction = NULL,
    .exitAction = NULL,
    .data = NULL,
    .transitions = (struct transition[]){
        {to_Number, bool_True, ALL_TO_ALL, &Parse_INFO},
        {to_Error, bool_True, ALL_TO_ALL, &Parse_ERROR}
    },
    .numTransitions = 2
};

struct FSM_state Parse_INFO = {
    .name = "info",
    .entryAction = NULL,
    .continuousAction = NULL,
    .exitAction = NULL,
    .data = NULL,
    .transitions = (struct transition[]){
        {to_Number, bool_True, ALL_TO_ALL, &Parse_CHECK},
        {to_Error, bool_True, ALL_TO_ALL, &Parse_ERROR}
    },
    .numTransitions = 2
};

struct FSM_state Parse_CHECK = {
    .name = "check",
    .entryAction = NULL,
    .continuousAction = NULL,
    .exitAction = NULL,
    .data = NULL,
    .transitions = (struct transition[]){
        {to_Number, bool_True, ALL_TO_ALL, &Parse_END},
        {to_Error, bool_True, ALL_TO_ERROR, &Parse_ERROR}
    },
    .numTransitions = 2
};

struct FSM_state Parse_END = {
    .name = "end",
    .entryAction = NULL,
    .continuousAction = NULL,
    .exitAction = NULL,
    .data = NULL,
    .transitions = (struct transition[]){
        {to_Number, bool_True, ALL_TO_ALL, &Parse_BEGIN},
        {to_Error, bool_True, ALL_TO_ERROR, &Parse_ERROR}
    },
    .numTransitions = 2
};


void Parse_Data_Example()
{
    struct FSM_state * DataParse = NULL;

    printf("Parse_ERROR:0x%p\n" , &Parse_ERROR);
    printf("Parse_BEGIN:0x%p\n" , &Parse_BEGIN);
    printf("Parse_NUMBER:0x%p\n", &Parse_NUMBER);
    printf("Parse_INFO:0x%p\n"  , &Parse_INFO);
    printf("Parse_CHECK:0x%p\n" , &Parse_CHECK);
    printf("Parse_END:0x%p\n\n" , &Parse_END);

    FSM_Init(&DataParse, &Parse_BEGIN);
    FSM_PrintInfo(DataParse);

    for (size_t i = 0; i < 1; i++)
    {
        printf("===============\n");
        FSM_Run(&DataParse, NULL);
        printf("===============\n");
    }
}








